<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-new.jpg?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-new.jpg?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="JVM调优,JVM参数,JVM,">










<meta name="description" content="从java文件到通过编译器编译成java字节码文件（也就是.class文件），这个过程是java编译过程；而java虚拟机执行的就是字节码文件。不论该字节码文件来自何方，由哪种编译器编译，甚至是手写字节码文件，只要符合java虚拟机的规范，那么它就能够执行该字节码文件。 首先我们来写一个简单的java源文件：12345678package come.demo;public class Demo &amp;">
<meta name="keywords" content="JVM调优,JVM参数,JVM">
<meta property="og:type" content="article">
<meta property="og:title" content="java字节码">
<meta property="og:url" content="https://lenoy.gitee.io/blog/JVM_class_code.html">
<meta property="og:site_name" content="Lenoy聆羽">
<meta property="og:description" content="从java文件到通过编译器编译成java字节码文件（也就是.class文件），这个过程是java编译过程；而java虚拟机执行的就是字节码文件。不论该字节码文件来自何方，由哪种编译器编译，甚至是手写字节码文件，只要符合java虚拟机的规范，那么它就能够执行该字节码文件。 首先我们来写一个简单的java源文件：12345678package come.demo;public class Demo &amp;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://lenoy.gitee.io/blog/JVM_class_code/1.gif">
<meta property="og:updated_time" content="2021-11-12T09:46:52.630Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="java字节码">
<meta name="twitter:description" content="从java文件到通过编译器编译成java字节码文件（也就是.class文件），这个过程是java编译过程；而java虚拟机执行的就是字节码文件。不论该字节码文件来自何方，由哪种编译器编译，甚至是手写字节码文件，只要符合java虚拟机的规范，那么它就能够执行该字节码文件。 首先我们来写一个简单的java源文件：12345678package come.demo;public class Demo &amp;">
<meta name="twitter:image" content="https://lenoy.gitee.io/blog/JVM_class_code/1.gif">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lenoy.gitee.io/blog/JVM_class_code.html">





  <title>java字节码 | Lenoy聆羽</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Lenoy聆羽</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">刘超的个人博客，java开发工程师</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lenoy.gitee.io/blog/blog/JVM_class_code.html">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="liuchao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lenoy聆羽">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">java字节码</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-08-12T00:00:00+08:00">
                2020-08-12
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/JVM/" itemprop="url" rel="index">
                    <span itemprop="name">JVM</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>从java文件到通过编译器编译成java字节码文件（也就是.class文件），这个过程是java编译过程；而java虚拟机执行的就是字节码文件。不论该字节码文件来自何方，由哪种编译器编译，甚至是手写字节码文件，只要符合java虚拟机的规范，那么它就能够执行该字节码文件。</p>
<p>首先我们来写一个简单的java源文件：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> come.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> a = <span class="number">1</span>;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">testMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>经过编译之后，可用得到java字节码文件：<br><img src="/blog/JVM_class_code/1.gif"><br>使用javap反编译之后可以得到字节码的可视化内容：<br><figure class="highlight smali"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">$  javap -verbose Demo.class                                                            </span><br><span class="line">Classfile /E:/cc/project/Demo.class                                                     </span><br><span class="line">  <span class="class">Last modified 2020-6-21;</span> size 360 bytes                                               </span><br><span class="line">  MD5 checksum 23702295a68a075c2b133a8ed4a46f25                                         </span><br><span class="line">  Compiled from <span class="string">"Demo.java"</span>                                                             </span><br><span class="line">public class com.demo.Demo                                                              </span><br><span class="line">  minor version: 0                                                                      </span><br><span class="line">  major version: 52                                                                     </span><br><span class="line">  flags: ACC_PUBLIC, ACC_SUPER                                                          </span><br><span class="line">Constant pool:                                                                          </span><br><span class="line">   <span class="comment">#1 = Methodref          #4.#17         // java/lang/Object."&lt;init&gt;":()V              </span></span><br><span class="line">   <span class="comment">#2 = Fieldref           #3.#18         // com/demo/Demo.a:I                          </span></span><br><span class="line">   <span class="comment">#3 = Class              #19            // com/demo/Demo                              </span></span><br><span class="line">   <span class="comment">#4 = Class              #20            // java/lang/Object                           </span></span><br><span class="line">   <span class="comment">#5 = Utf8               a                                                            </span></span><br><span class="line">   <span class="comment">#6 = Utf8               I                                                            </span></span><br><span class="line">   <span class="comment">#7 = Utf8               &lt;init&gt;                                                       </span></span><br><span class="line">   <span class="comment">#8 = Utf8               ()V                                                          </span></span><br><span class="line">   <span class="comment">#9 = Utf8               Code                                                         </span></span><br><span class="line">  <span class="comment">#10 = Utf8               LineNumberTable                                              </span></span><br><span class="line">  <span class="comment">#11 = Utf8               LocalVariableTable                                           </span></span><br><span class="line">  <span class="comment">#12 = Utf8               this                                                         </span></span><br><span class="line">  <span class="comment">#13 = Utf8               Lcom/demo/Demo;                                              </span></span><br><span class="line">  <span class="comment">#14 = Utf8               testMethod                                                   </span></span><br><span class="line">  <span class="comment">#15 = Utf8               SourceFile                                                   </span></span><br><span class="line">  <span class="comment">#16 = Utf8               Demo.java                                                    </span></span><br><span class="line">  <span class="comment">#17 = NameAndType        #7:#8          // "&lt;init&gt;":()V                               </span></span><br><span class="line">  <span class="comment">#18 = NameAndType        #5:#6          // a:I                                        </span></span><br><span class="line">  <span class="comment">#19 = Utf8               com/demo/Demo                                                </span></span><br><span class="line">  <span class="comment">#20 = Utf8               java/lang/Object                                             </span></span><br><span class="line">&#123;                                                                                       </span><br><span class="line"> <span class="keyword"> public</span> com.demo.Demo();                                                               </span><br><span class="line">    descriptor: ()V                                                                     </span><br><span class="line">    flags: ACC_PUBLIC                                                                   </span><br><span class="line">    Code:                                                                               </span><br><span class="line">      stack=2, locals=1, args_size=1                                                    </span><br><span class="line">         0: aload_0                                                                     </span><br><span class="line">         1: invokespecial <span class="comment">#1                  // Method java/lang/Object."&lt;init&gt;":()V   </span></span><br><span class="line">         4: aload_0                                                                     </span><br><span class="line">         5: iconst_1                                                                    </span><br><span class="line">         6: putfield      <span class="comment">#2                  // Field a:I                              </span></span><br><span class="line">         9:<span class="built_in"> return </span>                                                                     </span><br><span class="line">      LineNumberTable:                                                                  </span><br><span class="line">        line 3: 0                                                                       </span><br><span class="line">        line 4: 4                                                                       </span><br><span class="line">      LocalVariableTable:                                                               </span><br><span class="line">        Start  Length  Slot  Name   Signature                                           </span><br><span class="line">            0      10     0  this   <span class="class">Lcom/demo/Demo;</span>                                     </span><br><span class="line">                                                                                        </span><br><span class="line"> <span class="keyword"> protected</span> void testMethod();                                                          </span><br><span class="line">    descriptor: ()V                                                                     </span><br><span class="line">    flags: ACC_PROTECTED                                                                </span><br><span class="line">    Code:                                                                               </span><br><span class="line">      stack=0, locals=1, args_size=1                                                    </span><br><span class="line">         0:<span class="built_in"> return </span>                                                                     </span><br><span class="line">      LineNumberTable:                                                                  </span><br><span class="line">        line 7: 0                                                                       </span><br><span class="line">      LocalVariableTable:                                                               </span><br><span class="line">        Start  Length  Slot  Name   Signature                                           </span><br><span class="line">            0       1     0  this   <span class="class">Lcom/demo/Demo;</span>                                     </span><br><span class="line">&#125;                                                                                       </span><br><span class="line">SourceFile: <span class="string">"Demo.java"</span></span><br></pre></td></tr></table></figure></p>
<h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><p>Class文件格式采用一种类似于C语言结构体的伪结构来存储，这种伪结构中只有两种数据类型：无符号数和表。无符号数就是U1、U2、U4、U8来分别代表1个、2个、4个、8个字节。表是由多个无符号数或其他表构成的复合数据类型，以“_info”结尾。在表开始位置，通常会使用一个前置的容量计数器，因为表通常要描述数量不定的多个数据。</p>
<h3 id="字节码的结构"><a href="#字节码的结构" class="headerlink" title="字节码的结构"></a>字节码的结构</h3>
<table>
    <thead>
    <tr>
        <th width="15%">长度（字节）</th>
        <th width="20%">名称</th>
        <th>解释</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>4</td>
            <td>Magic Number</td>
            <td>魔数，用来区分文件类型的一种标志</td>
        </tr>
        <tr>
            <td>2+2</td>
            <td>Version</td>
            <td>版本号，javac编译器的版本</td>
        </tr>
        <tr>
            <td>2+n</td>
            <td>Contanst Pool</td>
            <td>常量池，存放文字字符串，常量值，当前类的类名，字段名，方法名，各个字段和方法的描述符，对当前类的字段和方法的引用信息，当前类中对其他类的引用信息等等</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Access_Flag</td>
            <td>访问标志，包括该Class文件是类还是接口，是否被定义成public，是否是abstract，是否被声明成final</td>
        </tr>
        <tr>
            <td>2</td>
            <td>This Class Name</td>
            <td>类索引用于确定此类的全限定名</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Super Class Name</td>
            <td>父类索引，用于确定此类的直接父类的全限定名</td>
        </tr>
        <tr>
            <td>2+n</td>
            <td>Interfaces</td>
            <td>接口表，用于表示该类的接口</td>
        </tr>
        <tr>
            <td>2+n</td>
            <td>Fields</td>
            <td>字段表，用于描述类和接口中声明的变量，这里的字段包含了类级别变量以及实例变量，但是不包括方法内部声明的局部变量</td>
        </tr>
        <tr>
            <td>2+n</td>
            <td>Methods</td>
            <td>方法表，用于描述类中定义的方法</td>
        </tr>
        <tr>
            <td>2+n</td>
            <td>Attributes</td>
            <td>属性表，用于描述类所携带的属性，包括Deprecated、InnerClass、SourceFile等等</td>
        </tr>
    </tbody>
</table>

<h4 id="Magic-Number-魔数"><a href="#Magic-Number-魔数" class="headerlink" title="Magic Number 魔数"></a>Magic Number 魔数</h4><p>什么是魔数？魔数是用来区分文件类型的一种标志，一般都是用文件的前几个字节来表示。比如此Demo中<strong>cafe babe</strong>表示的是class<br>文件，那么有人会问，文件类型可以通过文件名后缀来判断啊？是的，但是文件名是可以修改的（包括后缀），那么为了保证文件的安全性，将文件类型写在文件内部来保证不被篡改。<br>在class文件开头的四个字节，存放着class文件的魔数，这个魔数是class文件的标志，他是一个固定的值：0XCAFEBABE 。也就是说他是判断一个文件是不是class格式的文件的标准，如果开头四个字节不是0XCAFEBABE，那么就说明它不是class文件，不能被JVM识别。</p>
<h4 id="Version-版本号"><a href="#Version-版本号" class="headerlink" title="Version 版本号"></a>Version 版本号</h4><p>紧接着魔数的四个字节是class文件的此版本号和主版本号。 随着Java的发展， class文件的格式也会做相应的变动。 版本号标志着class文件在什么时候， 加入或改变了哪些特性。 举例来说， 不同版本的javac编译器编译的class文件， 版本号可能不同， 而不同版本的JVM能识别的class文件的版本号也可能不同， 一般情况下， 高版本的JVM能识别低版本的javac编译器编译的class文件， 而低版本的JVM不能识别高版本的javac编译器编译的class文件。 如果使用低版本的JVM执行高版本的class文件， JVM会抛出java.lang.UnsupportedClassVersionError 。</p>
<p>版本号含主版本号和次版本号，都是各占2个字节。在此Demo中为<strong>0000 0034</strong>。其中前面的0000是次版本号，后面的0034是主版本号。通过进制转换得到的是次版本号为0，主版本号为52。<br>从oracle官方网站我们能够知道，52对应的正式jdk1.8，而其次版本为0，所以该文件的版本为1.8.0。如果需要验证，可以在用java –version命令输出版本号，或者修改编译目标版本–target<br>重新编译，查看编译后的字节码文件版本号是否做了相应的修改。</p>
<h4 id="Contanst-Pool-常量池"><a href="#Contanst-Pool-常量池" class="headerlink" title="Contanst Pool 常量池"></a>Contanst Pool 常量池</h4><p>在class文件中，位于版本号后面的就是常量池相关的数据项。常量池是class文件中的一项非常重要的数据。常量池中存放了文字字符串，常量值，当前类的类名，字段名，方法名，各个字段和方法的描述符，对当前类的字段和方法的引用信息，当前类中对其他类的引用信息等等。常量池中几乎包含类中的所有信息的描述，class文件中的很多其他部分都是对常量池中的数据项的引用，比如后面要讲到的this_class, super_class, field_info, attribute_info等，另外字节码指令中也存在对常量池的引用，这个对常量池的引用当做字节码指令的一个操作数。此外，常量池中各个项也会相互引用。</p>
<p>常量池中的数据也是一项一项的，没有间隙的依次排放。常量池中各个数据项通过索引来访问，有点类似与数组，只不过常量池中的第一项的索引为1，而不为0，如果class文件中的其他地方引用了索引为0的常量池项，就说明它不引用任何常量池项。class文件中的每一种数据项都有自己的类型，相同的道理，常量池中的每一种数据项也有自己的类型。 </p>
<p>常量池中主要存储两类常量：</p>
<ol>
<li>字面量：字面量如文本字符串，Java中声明为final的常量值等。</li>
<li>符号引用：类和接口的全局限定名，字段的名称和描述符，方法的名称和描述符等。</li>
</ol>
<p>常量池中的数据项的类型如下表：<br>
<table>
    <thead>
    <tr>
        <th width="250px">常量</th>
        <th width="80px">项目</th>
        <th width="60px">类型</th>
        <th>描述</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td rowspan="3">CONSTANT_Utf8</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为1</td>
    </tr>
    <tr>
        <td>length</td>
        <td>U2</td>
        <td>UTF-8编码的Unicode字符串</td>
    </tr>
    <tr>
        <td>bytes</td>
        <td>U1</td>
        <td>长度为length的UTF-8编码的Unicode字符串</td>
    </tr>
    <tr>
        <td rowspan="2">CONSTANT_Integer</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为3</td>
    </tr>
    <tr>
        <td>bytes</td>
        <td>U4</td>
        <td>按照高位在前存储的int值</td>
    </tr>
    <tr>
        <td rowspan="2">CONSTANT_Float</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为4</td>
    </tr>
    <tr>
        <td>bytes</td>
        <td>U4</td>
        <td>按照高位在前存储的float值</td>
    </tr>
    <tr>
        <td rowspan="2">CONSTANT_Long</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为5</td>
    </tr>
    <tr>
        <td>bytes</td>
        <td>U8</td>
        <td>按照高位在前存储的long值</td>
    </tr>
    <tr>
        <td rowspan="2">CONSTANT_Double</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为6</td>
    </tr>
    <tr>
        <td>bytes</td>
        <td>U8</td>
        <td>按照高位在前存储的double值</td>
    </tr>
    <tr>
        <td rowspan="2">CONSTANT_Class</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为7</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向全限定名常量项的索引</td>
    </tr>
    <tr>
        <td rowspan="2">CONSTANT_String</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为8</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向字符串字面量的索引</td>
    </tr>
    <tr>
        <td rowspan="3">CONSTANT_Fieldref</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为9</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向声明字段的类或接口描述符CONSTANT_Class的索引项</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向名称及类型描述符CONSTANT_NameAndType的索引项</td>
    </tr>
    <tr>
        <td rowspan="3">CONSTANT_Methodref</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为10</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向声明方法的类描述符CONSTANT_Class的索引项</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向名称及类型描述符CONSTANT_NameAndType的索引项</td>
    </tr>
    <tr>
        <td rowspan="3">CONSTANT_InterfaceMethodref</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为11</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向声明方法的接口描述符CONSTANT_Class的索引项</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向名称及类型描述符CONSTANT_NameAndType的索引项</td>
    </tr>
    <tr>
        <td rowspan="3">CONSTANT_NameAndType</td>
        <td>tag</td>
        <td>U1</td>
        <td>值为12</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向该字段或方法名称常量项的索引</td>
    </tr>
    <tr>
        <td>index</td>
        <td>U2</td>
        <td>指向该字段或方法描述符常量项的索引</td>
    </tr>
    </tbody>
</table>
</p>
<p>上述常量都是以「CONSTANT」开头，以「info」结尾的常量名。每一个常量包含的信息的段都是不同的，我们可以根据每一个段自身的起始和结束位置是什么来进行分析。抽出两个代表性的常量进行解析：</p>
<h5 id="CONSTANT-Utf8-info"><a href="#CONSTANT-Utf8-info" class="headerlink" title="CONSTANT_Utf8_info"></a>CONSTANT_Utf8_info</h5><p>如果这个tag的值为1，它就表示的UTF-8编码的字符串；length，占2个字节，比如length值是4，表示的是从length的下后面读取4个字节长度的字符串。<br>这个就表示CONSTANT_Utf8_info的具体的文本内容。就是说根据length就能够知道接下来我要读取多少个字节才能读完，这些字节是由bytes来表示的。</p>
<h5 id="CONSTANT-Fieldref-info"><a href="#CONSTANT-Fieldref-info" class="headerlink" title="CONSTANT_Fieldref_info"></a>CONSTANT_Fieldref_info</h5><p>如果这个tag的值为9，有两个index值，第一个index代表的是指向声明字段的类或接口描述符CONSTANT_Class_info的索引项，第二个index代表的指向字段描述符CONSTANT_NameAndType_info的索引项。<br>具体可以理解为第一个index表示字段类型对应的类信息，指向CONSTANT_Class_info的索引项，可以具体看下CONSTANT_Class_info，值为7，它的index代表指向全限定名字符常量项的索引。然后第二个index表示了这个字段的名称，指向CONSTANT_NameAndType_info的索引项，CONSTANT_NameAndType_info的值为12，根据两个index的描述可以理解为要有字段或方法的名称以及字段或方法的描述符即可找到源码中对应的字段和方法。</p>
<h5 id="常量池结构分析"><a href="#常量池结构分析" class="headerlink" title="常量池结构分析"></a>常量池结构分析</h5><p>此Demo中紧跟版本号之后就是<strong>0015</strong>，代表常量池中有15项常量，索引值范围1~15。</p>
<ul>
<li><strong>#1 = 0a 00 04 00 11</strong><br>0a 值为10，表示CONSTANT_Methodref类型，两个index，各占2个字节，第一个index 00 04 ，值为4，表示索引到编号为4的CONSTANT_Class类型的常量，第二个index 00 01 ，值为1，表示索引到编号为17的CONSTANT_NameAndType类型的常量，即<strong>java/lang/Object.&lt;init>:()V</strong></li>
<li><strong>#2 = 09 00 03 00 12</strong><br>09 值为9，表示CONSTANT_Fieldref类型，两个index，各占2个字节，第一个index 00 03 ，值为3，表示索引到编号为3的CONSTANT_Class类型的常量，第二个index 00 01，值为17，表示索引到编号为17的CONSTANT_NameAndType类型的常量，即<strong>com/demo/Demo.a:I</strong></li>
<li><strong>#3 = 07 00 13</strong><br>07 表示CONSTANT_Class类型，00 13 ，值为19，全限定类名索引到编号为19的常量，即<strong>com/demo/Demo</strong></li>
<li><strong>#4 = 07 00 14</strong><br>07 表示CONSTANT_Class类型，00 14 ，值为20，全限定类名索引到编号为20的常量，即<strong>java/lang/Object</strong></li>
<li><strong>#5 = 01 00 01 61</strong><br>01 表示CONSTANT_Utf8类型，00 01 表示长度为1个字节，61 值为97，表示<strong>a</strong></li>
<li><strong>#6 = 01 00 01 49</strong><br>01 表示CONSTANT_Utf8类型，00 01 表示长度为1个字节，49 值为73，表示<strong>I</strong></li>
<li><strong>#7 = 01 00 06 3c 69 6e 69 74 3e</strong><br>CONSTANT_Utf8类型，长度为6个字节，3c 69 6e 69 74 3e 表示<strong>&lt;init></strong></li>
<li><strong>#8 = 01 00 03 28 29 56</strong><br>CONSTANT_Utf8类型，长度为3个字节，表示<strong>()V</strong></li>
<li><strong>#9 = 01 00 04 43 6f 64 65</strong><br>CONSTANT_Utf8类型，长度为4个字节，表示<strong>Code</strong></li>
<li><strong>#10 = 01 00 0f 4c 69 6e 65 4e 75 6d 62 65 72 54 61 62 6c 65</strong><br>CONSTANT_Utf8类型，长度为15个字节，表示<strong>LineNumberTable</strong></li>
<li><strong>#11 = 01 00 12 4c 6f 63 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65</strong><br>CONSTANT_Utf8类型，长度为18个字节，表示<strong>LocalVariableTable</strong></li>
<li><strong>#12 = 01 00 04 74 68 69 73</strong><br>CONSTANT_Utf8类型，长度为4个字节，表示<strong>this</strong></li>
<li><strong>#13 = 01 00 0f 4c 63 6f 6d 2f 64 65 6d 6f 2f 44 65 6d 6f 3b</strong><br>CONSTANT_Utf8类型，长度为15个字节，表示<strong>Lcom/demo/Demo</strong>;</li>
<li><strong>#14 = 01 00 0a 74 65 73 74 4d 65 74 68 6f 64</strong><br>CONSTANT_Utf8类型，长度为10个字节，表示<strong>testMethod</strong></li>
<li><strong>#15 = 01 00 0a 53 6f 75 72 63 65 46 69 6c 65</strong><br>CONSTANT_Utf8类型，长度为10个字节，表示<strong>SourceFile</strong></li>
<li><strong>#16 = 01 00 09 44 65 6d 6f 2e 6a 61 76 61</strong><br>CONSTANT_Utf8类型，长度为9个字节，表示<strong>Demo.java</strong></li>
<li><strong>#17 = 0c 00 07 00 08</strong><br>0c 表示CONSTANT_NameAndType类型，00 07 表示索引到编号为7的字段或方法名称常量，00 08 表示索引到编号为8的字段或方法名称常量，即<strong>&lt;init>:()V</strong></li>
<li><strong>#18 = 0c 00 05 00 06</strong><br>表示CONSTANT_NameAndType类型，索引到编号为7的字段或方法名称常量和索引到编号为8的字段或方法名称常量，即<strong>a:I</strong></li>
<li><strong>#19 = 01 00 0d 63 6f 6d 2f 64 65 6d 6f 2f 44 65 6d 6f</strong><br>CONSTANT_Utf8类型，长度为13个字节，表示<strong>com/demo/Demo</strong></li>
<li><strong>#20 = 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74</strong><br>CONSTANT_Utf8类型，长度为16个字节，表示<strong>java/lang/Object</strong></li>
</ul>
<h4 id="Access-Flag-访问标志"><a href="#Access-Flag-访问标志" class="headerlink" title="Access_Flag 访问标志"></a>Access_Flag 访问标志</h4><p>访问标志信息包括该Class文件是类还是接口，是否被定义成public，是否是abstract，如果是类，是否被声明成final。<br>
<table>
    <thead>
    <tr>
        <th width="25%">标记名</th>
        <th width="15%">值</th>
        <th>含义</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>ACC_PUBLIC</td>
            <td>0x0001</td>
            <td>可以被包的类外访问</td>
        </tr>
        <tr>
            <td>ACC_PRIVATE</td>
            <td>0x0002</td>
            <td>标识private</td>
        </tr>
        <tr>
            <td>ACC_PROTECTED</td>
            <td>0x0004</td>
            <td>标识protected</td>
        </tr>
        <tr>
            <td>ACC_STATIC</td>
            <td>0x0008</td>
            <td>标识static</td>
        </tr>
        <tr>
            <td>ACC_FINAL</td>
            <td>0x0010</td>
            <td>不允许有子类</td>
        </tr>
        <tr>
            <td>ACC_SUPER</td>
            <td>0x0020</td>
            <td>当用到invokespecial指令时，需要特殊处理的父类方法</td>
        </tr>
        <tr>
            <td>ACC_VOLATILE</td>
            <td>0x0040</td>
            <td>标识volatile</td>
        </tr>
        <tr>
            <td>ACC_TRANSIENT</td>
            <td>0x0080</td>
            <td>标识transient</td>
        </tr>
        <tr>
            <td>ACC_INTERFACE</td>
            <td>0x0200</td>
            <td>标识定义的是接口而不是类</td>
        </tr>
        <tr>
            <td>ACC_ABSTRACT</td>
            <td>0x0400</td>
            <td>不能被实例化</td>
        </tr>
        <tr>
            <td>ACC_SYNTHETIC</td>
            <td>0x1000</td>
            <td>标识并非Java源码生成的代码</td>
        </tr>
        <tr>
            <td>ACC_ANNOTATION</td>
            <td>0x2000</td>
            <td>标识注解类型</td>
        </tr>
        <tr>
            <td>ACC_ENUM</td>
            <td>0x4000</td>
            <td>标识枚举类型</td>
        </tr>
    </tbody>
</table>
</p>
<p>此Demo中访问标识为<strong>00 21</strong>，0x0021在 Access Flags 中是0x0020和0x0001的并集,表示 ACC PUBLIC与 ACC SUPER合并的结果。说明该类的声明修饰是public并且继承Object。</p>
<h4 id="This-Class-Name-类索引"><a href="#This-Class-Name-类索引" class="headerlink" title="This Class Name 类索引"></a>This Class Name 类索引</h4><p>类索引用于确定此类的全限定名，此Demo中类索引为<strong>00 03</strong>，表示引用常量池的第3个常量，即<strong>com/demo/Demo</strong></p>
<h4 id="Super-Class-Name-父类索引"><a href="#Super-Class-Name-父类索引" class="headerlink" title="Super Class Name 父类索引"></a>Super Class Name 父类索引</h4><p>父类索引用于确定此类的直接父类的全限定名，此Demo中父类索引为<strong>00 04</strong>，表示引用常量池的第4个常量，即<strong>java/lang/Object</strong></p>
<h4 id="Interfaces-接口表"><a href="#Interfaces-接口表" class="headerlink" title="Interfaces 接口表"></a>Interfaces 接口表</h4><p>用于表示该类的接口，包括两部分，第一个是interfaces_count（接口个数，2个字节)，第二部分interfaces（接口名，n*2个字节，n表示接口个数）。<br>此Demo中interfaces_count为<strong>00 00</strong>，表示没有接口</p>
<h4 id="Fields-字段表"><a href="#Fields-字段表" class="headerlink" title="Fields 字段表"></a>Fields 字段表</h4><p>用于描述类和接口中声明的变量，这里的字段包含了类级别变量以及实例变量，但是不包括方法内部声明的局部变量。<br>字段表包括两部分，第一个是fields_count（字段个数，2个字节），第二部分fields（字段名，n*2个字节，n表示字段个数）。<br>此Demo中fields_count为<strong>00 01</strong>，表示有1个字段。</p>
<h5 id="字段表结构"><a href="#字段表结构" class="headerlink" title="字段表结构"></a>字段表结构</h5>
<table>
    <thead>
    <tr>
        <th>类型</th>
        <th>名称</th>
        <th>数量</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>access_flags</td>
            <td>1</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>name_index</td>
            <td>1</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>descriptor_index</td>
            <td>1</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>attributes_count</td>
            <td>1</td>
        </tr>
        <tr>
            <td>attributes_info</td>
            <td>attributes</td>
            <td>attributes_count</td>
        </tr>
    </tbody>
</table>

<ul>
<li>access_flags，访问标志符，如public、private、protected、final、abstract等等</li>
<li>name_index，对常量池的一个有效索引。常量池在该索引处的项必须是CONSTANT_Utf8_info结构，表示一个有效的字段的非全限定名</li>
<li>descriptor_index，对常量池的一个有效索引。常量池在该索引处的项必须是CONSTANT_Utf8_info结构，表示一个有效的字段的描述符</li>
<li>attributes_count是字段的独有的信息，如果值是0，后面的attributes也就不存在了</li>
<li>aattributes是字段的独有的信息，attributes 表的每一个成员的值必须是 attribute结构，一个字段可以有任意个关联属性。<blockquote>
<p>在JVM规范中，每个变量/字段都有描述信息，描述信息主要作用是描述字段的数据类型、方法的参数列表（包括数量，类型与顺序）与返回值。根据描述符规则，基本数据类型和代表无返回值的void类型都用一个大写字符来表示，对象类型则使用字符L加对象的全限定名称来表示。为了压缩字节码文件的体积，对于基本数据类型，JVM都只使用一个大写字母表示，如下所示：B-byte、C-char、D-double、F-float、I-int、J-long、S-short、Z-boolean、V-void、L-对象类型，如Ljava/lang/String。<br>对于数组类型来说，每一个纬度使一个前置的[表示，如int[]被记录为[I，String[][]被记录为[[Ljava/lang/String;。</p>
</blockquote>
</li>
</ul>
<h5 id="字段表解析"><a href="#字段表解析" class="headerlink" title="字段表解析"></a>字段表解析</h5><ul>
<li><p><strong>00 02 00 05 00 06 00 00</strong></p>
<ul>
<li>00 02 表示ACC_PRIVATE</li>
<li>00 05 表示引用常量池的第5个常量，即<strong>a</strong>，表示字段名称是a</li>
<li>00 06 表示引用常量池的第6个常量，即<strong>I</strong>，表示字段类型是int</li>
<li>00 00 表示字段的附加属性数量为0</li>
</ul>
<p>所以此字段为<strong>private int a</strong>。</p>
</li>
</ul>
<h4 id="Methods-方法表"><a href="#Methods-方法表" class="headerlink" title="Methods 方法表"></a>Methods 方法表</h4><p>用于描述类中定义的方法。<br>方法表包括两部分，第一个是methods_count（方法个数），第二部分methods（方法名）。<br>此Demo中methods_count为<strong>00 02</strong>，表示有2个方法。</p>
<h5 id="方法表结构"><a href="#方法表结构" class="headerlink" title="方法表结构"></a>方法表结构</h5>
<table>
    <thead>
    <tr>
        <th>类型</th>
        <th>名称</th>
        <th>数量</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>access_flags</td>
            <td>1</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>name_index</td>
            <td>1</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>descriptor_index</td>
            <td>1</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>attributes_count</td>
            <td>1</td>
        </tr>
        <tr>
            <td>attributes_info</td>
            <td>attributes</td>
            <td>attributes_count</td>
        </tr>
    </tbody>
</table>

<h5 id="方法表解析"><a href="#方法表解析" class="headerlink" title="方法表解析"></a>方法表解析</h5><p><strong>00 01 00 07 00 08 00 01</strong></p>
<ul>
<li>00 01 表示ACC_PUBLIC</li>
<li>00 07 表示引用常量池的第7个常量，即<strong>&lt;init></strong>，表示方法名称是&lt;init></li>
<li>00 08 表示引用常量池的第8个常量，即<strong>()V</strong>，表示方法描述是()V，即返回值为void</li>
<li>00 01 表示方法的附加属性数量为1</li>
</ul>
<p>方法中的每个属性都是一个attribut_info结构：<br>
<table>
    <thead>
    <tr>
        <th>类型</th>
        <th>名称</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>attribute_name_index</td>
        </tr>
        <tr>
            <td>U4</td>
            <td>attribute_length</td>
        </tr>
        <tr>
            <td>U1</td>
            <td>info</td>
        </tr>
    </tbody>
</table>
<br>和字段表结构基本相同。</p>
<p>接下来是这个方法的属性：<strong>00 09 00 00 00 38</strong></p>
<ul>
<li>00 09 表示引用常量池的第9个常量，即<strong>Code</strong>，表示这个属性的类型是Code类型</li>
<li>00 00 00 38 属性长度为56<blockquote>
<p>Java程序方法体中的代码经过Javac编译处理后，最终变为字节码指令存储在Code属性中。Code属性出现在方法表的属性集合中，但是并非所有的方法表都有这个属性。例如接口或类中的方法就不存在Code属性了。</p>

<table>
    <thead>
    <tr>
        <th width="25%">类型</th>
        <th width="25%">名称</th>
        <th>解释</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>attribute_name_index</td>
            <td>属性名称的索引</td>
        </tr>
        <tr>
            <td>U4</td>
            <td>attribute_length</td>
            <td>表示attribute所包含的字节数</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>max_stack</td>
            <td>表示这个方法运行的任何时刻所能达到的操作数栈的最大深度</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>max_locals</td>
            <td>表示方法执行运行期间创建的局部变量的数目，包含用来表示传入的参数的局部变量</td>
        </tr>
        <tr>
            <td>U4</td>
            <td>code_length</td>
            <td>表示该方法代码所包含的字节码的字节数</td>
        </tr>
        <tr>
            <td>U1</td>
            <td>code</td>
            <td>表示该方法所包含的具体的指令码</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>exception_table_length</td>
            <td>表示该方法所处理的异常信息数量</td>
        </tr>
        <tr>
            <td>exception_info</td>
            <td>exception_table</td>
            <td>具体的异常信息</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>attributes_count</td>
            <td>附加属性数量</td>
        </tr>
        <tr>
            <td>attribute_info</td>
            <td>attributes</td>
            <td>具体的附加属性信息</td>
        </tr>
    </tbody>
</table>

<p>exception_table是一个类型为exception的集合，每一种受查异常使用一个exception项表示，exception是一个指向常量池中CONSTANT_Class_info<br>型常量的索引，代表了该受查异常的类型。exception结构：</p>

<table>
    <thead>
    <tr>
        <th width="15%">类型</th>
        <th width="20%">名称</th>
        <th>解释</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>start_pc</td>
            <td>表示在code数组中的从start_pc到end_pc处包含start_pc,不包含end_pc)的指令抛出的异常会由这个表项来处理</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>end_pc</td>
            <td>表示在code数组中的从start_pc到end_pc处包含start_pc,不包含end_pc)的指令抛出的异常会由这个表项来处理</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>handler_pc</td>
            <td>表示处理异常的代码的开始处</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>catch_type</td>
            <td>表示会被处理的异常类型，它指向量池里的一个异常类，当catch_type为0时，表示处理所有的异常</td>
        </tr>
    </tbody>
</table>

</blockquote>
</li>
</ul>
<p>Code的具体内容：<strong>00 02 00 01 00 00 00 0a 2a b7 00 01 2a 04 b5 00 02 b1 00 00 00 02 00 0a 00 00 00 0a 00 02 00 00 00 03 00 04 00 04 00 0b 00 00 00 0c 00 01 00 00 00 0a 00 0c 00 0d 00 00</strong></p>
<ul>
<li>00 02 表示这个方法运行的任何时刻所能达到的操作数栈的最大深度为2</li>
<li>00 01 表示方法执行运行期间创建的局部变量的数目为1</li>
<li>00 00 00 0a 表示该方法代码所包含的字节码的字节数为10</li>
<li>00 00 00 0a 表示该方法代码所包含的字节码的字节数为10</li>
<li>2a b7 00 01 2a 04 b5 00 02 b1 表示该方法所包含的具体的指令码<ul>
<li>2a aload</li>
<li>b7 00 01 invokespecial</li>
<li>2a aload</li>
<li>04 iconst</li>
<li>b5 00 02 putfield</li>
<li>b1 return</li>
</ul>
</li>
<li>00 00 表示该方法所包含的异常信息数为0</li>
<li>00 02 表示该方法所包含的附加属性为2个</li>
<li>00 0a 第一个附加属性，索引到常量池#10常量，即<strong>LineNumberTable</strong></li>
<li><p>00 00 00 0a 第一个附加属性的字节数为10</p>
<blockquote>
<p>LineNumberTable的作用是用来表示code数组中的字节码和java代码行数之间的关系，结构：</p>

<table>
    <thead>
    <tr>
        <th width="25%">类型</th>
        <th width="25%">名称</th>
        <th>解释</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>attribute_name_index</td>
            <td>属性名称的索引</td>
        </tr>
        <tr>
            <td>U4</td>
            <td>attribute_length</td>
            <td>表示attribute所包含的字节数</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>line_number_table_length</td>
            <td>表长度</td>
        </tr>
    </tbody>
</table>

<p>line_number用来记录字节码行号和java代码行数之间的关系，一项记录一对关系，结构：</p>

<table>
    <thead>
    <tr>
        <th width="25%">类型</th>
        <th width="25%">名称</th>
        <th>解释</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>start_pc</td>
            <td>字节码行号</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>line_number</td>
            <td>java代码行号</td>
        </tr>
    </tbody>
</table>

<p>LineNumberTable的具体内容：<strong>00 0a 00 00 00 0a 00 02 00 00 00 03 00 04 00 04</strong></p>
<ul>
<li>00 0a 属性名称，索引到常量池#10常量，即<strong>LineNumberTable</strong></li>
<li>00 00 00 0a 属性的字节数为10</li>
<li>00 02 LineNumberTable长度为2</li>
<li>00 00 00 03 0000对应0003</li>
<li>00 04 00 04 0004对应0004</li>
</ul>
</blockquote>
</li>
<li><p>00 0b 第二个附加属性，索引到常量池#11常量，即<strong>LocalVariableTable</strong></p>
</li>
<li>00 00 00 0c 第二个附加属性字节数为12<blockquote>
<p>LocalVariableTable的作用是用来保存方法内部的局部变量即局部变量表，结构和LineNumberTable一样，只是表中的保存项变为了局部变量，结构</p>

<table>
    <thead>
    <tr>
        <th width="25%">类型</th>
        <th width="25%">名称</th>
        <th>解释</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>start_pc</td>
            <td>局部变量的开始位置</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>length</td>
            <td>局部变量的结束位置</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>contanst_index</td>
            <td>对应常量池里边的索引</td>
        </tr>
        <tr>
            <td>U2</td>
            <td>descriptor_index</td>
            <td>对局部变量的一个描述的索引</td>
        </tr>
    </tbody>
</table>

<p>LocalVariableTable的具体内容：<strong>00 0b 00 00 00 0c 00 01 00 00 00 0a 00 0c 00 0d 00 00</strong></p>
<ul>
<li>00 0b 属性名称，索引到常量池#11常量，即<strong>LocalVariableTable</strong></li>
<li>00 00 00 0c 属性字节数为12</li>
<li>00 01 LocalVariableTable长度为1</li>
<li>00 00 开始位置：0</li>
<li>00 0a 结束位置：10</li>
<li>00 0c 索引到常量池#12常量，即<strong>this</strong><blockquote>
<p>对于实例方法（非static）是默认隐式传递进来的。实例方法至少有一个局部变量，那就是this</p>
</blockquote>
</li>
<li>00 0d 对局部变量的一个描述，索引到常量池#13常量，即<strong>Lcom/demo/Demo</strong></li>
<li>00 00 jdk加入的校验位</li>
</ul>
</blockquote>
</li>
</ul>
<p>方法表的第一项所有分析完成，总结：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">00 01	ACC_PUBLIC</span><br><span class="line">00 07	&lt;init&gt;</span><br><span class="line">00 08	()V</span><br><span class="line">00 01	attributes_count</span><br><span class="line">00 09	Code</span><br><span class="line">00 00	00 38 字节码长度56</span><br><span class="line">00 02	max_stack</span><br><span class="line">00 01	max_locals</span><br><span class="line">00 00 00 0a		代码字节码长度10</span><br><span class="line">2a b7 00 01 2a 04 b5 00 02 b1	代码字节码</span><br><span class="line">00 00	exception_table_length</span><br><span class="line">00 02	附加属性数量</span><br><span class="line">00 0a	LineNumberTable</span><br><span class="line">00 00 00 0a 	属性的字节数为10</span><br><span class="line">00 02 	LineNumberTable长度2</span><br><span class="line">00 00 00 03		0-3</span><br><span class="line">00 04 00 04 	4-4</span><br><span class="line">00 0b 	LocalVariableTable</span><br><span class="line">00 00 00 0c 	属性的字节数为12</span><br><span class="line">00 01	LocalVariableTable长度1</span><br><span class="line">00 00	start_pc</span><br><span class="line">00 0a	length</span><br><span class="line">00 0c	contanst_index常量索引this</span><br><span class="line">00 0d	描述Lcom/demo/Demo</span><br><span class="line">00 00	校验</span><br></pre></td></tr></table></figure></p>
<p>根据方法表第一项的分析，我们可以得出方法表的第二项：<br><strong>00 04 00 0e 00 08 00 01 00 09 00 00 00 2b 00 00 00 01 00 00 00 01 b1 00 00 00 02 00 0a 00 00 00 06 00 01 00 00 00 07 00 0b 00 00 00 0c 00 01 00 00 00 01 00 0c 00 0d 00 00</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">00 04 ACC_PROTECTED</span><br><span class="line">00 0e testMethod</span><br><span class="line">00 08 ()V</span><br><span class="line">00 01 attributes_count</span><br><span class="line">00 09 Code</span><br><span class="line">00 00 00 2b 字节码长度43</span><br><span class="line">00 00	max_stack</span><br><span class="line">00 01	max_locals</span><br><span class="line">00 00 00 01		代码字节码长度1</span><br><span class="line">b1	具体代码字节码</span><br><span class="line">00 00	exception_table_length</span><br><span class="line">00 02	附加属性数量</span><br><span class="line">00 0a	LineNumberTable</span><br><span class="line">00 00 00 06		属性的字节数为6</span><br><span class="line">00 01	LineNumberTable长度1</span><br><span class="line">00 00 00 07		0-7</span><br><span class="line">00 0b	LocalVariableTable</span><br><span class="line">00 00 00 0c		属性的字节数为12</span><br><span class="line">00 01	LocalVariableTable长度1</span><br><span class="line">00 00	start_pc</span><br><span class="line">00 01	length</span><br><span class="line">00 0c	contanst_index常量索引this</span><br><span class="line">00 0d	描述Lcom/demo/Demo</span><br><span class="line">00 00	校验</span><br></pre></td></tr></table></figure></p>
<h4 id="Attributes-属性表"><a href="#Attributes-属性表" class="headerlink" title="Attributes 属性表"></a>Attributes 属性表</h4><p>在class文件，类文件、字段表，方法表都可以携带自己的属性表集合（像前面方法表的时候就用到＂code＂这个属性表）以用于描述某些场景专有的信息。而类所携带的属性，就在类的属性表中记录。<br>虚拟机中预定义的属性：<br>
<table>
    <thead>
    <tr>
        <th>属性名称</th>
        <th>使用位置</th>
        <th>含义</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>Code</td>
            <td>方法表</td>
            <td>Java代码编译成的字节码指令</td>
        </tr>
        <tr>
            <td>ConstantValue</td>
            <td>字段表</td>
            <td>final关键字定义的常量池</td>
        </tr>
        <tr>
            <td>Deprecated</td>
            <td>类，方法，字段表</td>
            <td>被声明为deprecated的方法和字段</td>
        </tr>
        <tr>
            <td>Exceptions</td>
            <td>方法表</td>
            <td>方法抛出的异常</td>
        </tr>
        <tr>
            <td>EnclosingMethod</td>
            <td>类文件</td>
            <td>仅当一个类为局部类或者匿名类是才能拥有这个属性，这个属性用于标识这个类所在的外围方法</td>
        </tr>
        <tr>
            <td>InnerClass</td>
            <td>类文件</td>
            <td>内部类列表</td>
        </tr>
        <tr>
            <td>LineNumberTable</td>
            <td>Code属性</td>
            <td>Java源码的行号与字节码指令的对应关系</td>
        </tr>
        <tr>
            <td>LocalVariableTable</td>
            <td>Code属性</td>
            <td>方法的局部变量描述</td>
        </tr>
        <tr>
            <td>StackMapTable</td>
            <td>Code属性</td>
            <td>JDK1.6中新增的属性，供新的类型检查检验器检查和处理目标方法的局部变量和操作数有所需要的类是否匹配</td>
        </tr>
        <tr>
            <td>Signature</td>
            <td>类，方法表，字段表</td>
            <td>用于支持泛型情况下的方法签名</td>
        </tr>
        <tr>
            <td>SourceFile</td>
            <td>类文件</td>
            <td>记录源文件名称</td>
        </tr>
        <tr>
            <td>SourceDebugExtension</td>
            <td>类文件</td>
            <td>用于存储额外的调试信息</td>
        </tr>
        <tr>
            <td>Synthetic</td>
            <td>类，方法表，字段表</td>
            <td>标志方法或字段为编译器自动生成的</td>
        </tr>
        <tr>
            <td>LocalVariableTypeTable</td>
            <td>类</td>
            <td>使用特征签名代替描述符，是为了引入泛型语法之后能描述泛型参数化类型而添加</td>
        </tr>
        <tr>
            <td>RuntimeVisibleAnnotations</td>
            <td>类，方法表，字段表</td>
            <td>为动态注解提供支持</td>
        </tr>
        <tr>
            <td>RuntimeInvisibleAnnotations</td>
            <td>表，方法表，字段表</td>
            <td>用于指明哪些注解是运行时不可见的</td>
        </tr>
        <tr>
            <td>RuntimeVisibleParameterAnnotation</td>
            <td>方法表</td>
            <td>作用与RuntimeVisibleAnnotations属性类似，只不过作用对象为方法</td>
        </tr>
        <tr>
            <td>RuntimeInvisibleParameterAnnotation</td>
            <td>方法表</td>
            <td>作用与RuntimeVisibleAnnotations属性类似，只不过作用对象为方法的某个参数</td>
        </tr>
        <tr>
            <td>AnnotationDefault</td>
            <td>方法表</td>
            <td>用于记录注解类元素的默认值</td>
        </tr>
        <tr>
            <td>BootstrapMethods</td>
            <td>类文件</td>
            <td>用于保存invokeddynamic指令引用的引导方式限定符</td>
        </tr>
    </tbody>
</table>
<br>对于每个属性，它的名称需要从常量池中引用一个CONSTANT_utf8_info类型的常量类表示，而属性值的结构则是完全自定义的，只需要通过一个u4的长度属性区说明属性值所占用的位数即可：<br>
<table>
    <thead>
    <tr>
        <th>类型</th>
        <th>名称</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>U2</td>
            <td>attribute_name_index</td>
        </tr>
        <tr>
            <td>U4</td>
            <td>attribute_length</td>
        </tr>
        <tr>
            <td>U1</td>
            <td>info</td>
        </tr>
    </tbody>
</table>
</p>
<p>至此，整个class的大概分析就完成了。</p>

      
    </div>
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:16px;">--------------<i class="fa fa-paw"></i>本文结束<i class="fa fa-paw"></i>--------------</div>
    
</div>
      
    </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>觉得文章不错就打赏一下哟~~~</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/blog/images/wechatpay.png" alt="liuchao 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/blog/images/alipay.png" alt="liuchao 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
<!--
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    liuchao
  </li>
-->
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://lenoy.gitee.io/blog/JVM_class_code.html" title="java字节码">https://lenoy.gitee.io/blog/JVM_class_code.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/JVM调优/" rel="tag"><i class="fa fa-tag"></i> JVM调优</a>
          
            <a href="/blog/tags/JVM参数/" rel="tag"><i class="fa fa-tag"></i> JVM参数</a>
          
            <a href="/blog/tags/JVM/" rel="tag"><i class="fa fa-tag"></i> JVM</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/Distributed_mq.html" rel="next" title="关于消息队列的思与考（转载）">
                <i class="fa fa-chevron-left"></i> 关于消息队列的思与考（转载）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/Java_reference.html" rel="prev" title="Java的四种引用方式">
                Java的四种引用方式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/head.jpg" alt="liuchao">
            
              <p class="site-author-name" itemprop="name">liuchao</p>
              <p class="site-description motion-element" itemprop="description">好记性不如烂笔头，记下来，迈出去</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">62</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">83</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#数据类型"><span class="nav-number">1.</span> <span class="nav-text">数据类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字节码的结构"><span class="nav-number">2.</span> <span class="nav-text">字节码的结构</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Magic-Number-魔数"><span class="nav-number">2.1.</span> <span class="nav-text">Magic Number 魔数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Version-版本号"><span class="nav-number">2.2.</span> <span class="nav-text">Version 版本号</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Contanst-Pool-常量池"><span class="nav-number">2.3.</span> <span class="nav-text">Contanst Pool 常量池</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#CONSTANT-Utf8-info"><span class="nav-number">2.3.1.</span> <span class="nav-text">CONSTANT_Utf8_info</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#CONSTANT-Fieldref-info"><span class="nav-number">2.3.2.</span> <span class="nav-text">CONSTANT_Fieldref_info</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#常量池结构分析"><span class="nav-number">2.3.3.</span> <span class="nav-text">常量池结构分析</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Access-Flag-访问标志"><span class="nav-number">2.4.</span> <span class="nav-text">Access_Flag 访问标志</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#This-Class-Name-类索引"><span class="nav-number">2.5.</span> <span class="nav-text">This Class Name 类索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Super-Class-Name-父类索引"><span class="nav-number">2.6.</span> <span class="nav-text">Super Class Name 父类索引</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Interfaces-接口表"><span class="nav-number">2.7.</span> <span class="nav-text">Interfaces 接口表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Fields-字段表"><span class="nav-number">2.8.</span> <span class="nav-text">Fields 字段表</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#字段表结构"><span class="nav-number">2.8.1.</span> <span class="nav-text">字段表结构</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#字段表解析"><span class="nav-number">2.8.2.</span> <span class="nav-text">字段表解析</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Methods-方法表"><span class="nav-number">2.9.</span> <span class="nav-text">Methods 方法表</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#方法表结构"><span class="nav-number">2.9.1.</span> <span class="nav-text">方法表结构</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#方法表解析"><span class="nav-number">2.9.2.</span> <span class="nav-text">方法表解析</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Attributes-属性表"><span class="nav-number">2.10.</span> <span class="nav-text">Attributes 属性表</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-id-card"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liuchao</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a></div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript">
  !function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
</script>
